from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.cache.decorators import cache_response
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from .models import Area
from .serializers import AreaSerializer, SubAreaSerializer

class AreasViewSet(CacheResponseMixin,ReadOnlyModelViewSet):
    """
    行政区划信息
    """
    pagination_class = None

    # @cache_response(timeout=60*60,cache='default')
    # @cache_response()
    def get_queryset(self):

        if self.action == 'list':
            return Area.objects.filter(parent = None)
        else:
            return Area.objects.all()

    def get_serializer_class(self):
        if self.action == 'list':
            return AreaSerializer
        else:
            return SubAreaSerializer
        pass



    # pagination_class = None  # 区划信息不分页
    #
    # def get_queryset(self):
    #     """
    #     提供数据集
    #     """
    #     if self.action == 'list':
    #         return Area.objects.filter(parent=None)
    #     else:
    #         return Area.objects.all()
    #
    # def get_serializer_class(self):
    #     """
    #     提供序列化器
    #     """
    #     if self.action == 'list':
    #         return AreaSerializer
    #     else:
    #         return SubAreaSerializer